//
//  DLPayInputAlertView.m
//  DGGUISpaceSDK
//
//  Created by 伍明鹏 on 2019/12/12.
//


#import "DLPayInputAlertView.h"

#define  boxWidth  44          //密码框的宽度

@interface DLPayInputAlertView()<UITextFieldDelegate>

//黑色半透明背景
@property (nonatomic, strong) UIView *viewBG;
//接受密码输入的文本框，看不见
@property (nonatomic, strong) UITextField *payTF;
//视图
@property (nonatomic, strong) UIView *payView;
//支付密码位数
@property (nonatomic, assign) NSInteger payNum;

@end

@implementation DLPayInputAlertView

- (instancetype)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle payNum:(NSInteger)payNum{
    if(self == [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {
        [self setupUIWithTitle:title WithSubTitle:subTitle payNum:payNum];
    }
    return self;
}

-(void)setupUIWithTitle:(NSString *)title WithSubTitle:(NSString *)subTitle payNum:(NSInteger)payNum{
    self.payNum = payNum;
    
    // 蒙版
    UIView *viewBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    viewBG.backgroundColor = [UIColor blackColor];
    viewBG.alpha = 0.7;
    viewBG.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromView)];
    [viewBG addGestureRecognizer:tap];
    _viewBG = viewBG;
    [self addSubview:_viewBG];
    
    // 输入密码View
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 163 )];
    view.center = CGPointMake(viewBG.center.x, viewBG.frame.size.height/5*2);
    [view.layer setCornerRadius:15];
    [view.layer setMasksToBounds:YES];
    view.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    view.userInteractionEnabled = YES;
    self.payView = view;
    self.payView.alpha = 0;
    [self addSubview:self.payView];
    
    // 标题
    UILabel *lable_title = [[UILabel alloc]init];
    lable_title.frame = CGRectMake(10, 20, view.frame.size.width-20, 15);
    lable_title.text = [NSString stringWithFormat:@"%@",title];
    lable_title.textAlignment=1;
    lable_title.font = [UIFont boldSystemFontOfSize:18.0f];
    lable_title.textColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x333333] darkColor:[UIColor colorWithHex:0xE0E0E0]];
    [self.payView addSubview:lable_title];
    
    // TF文本框
    self.payTF = [[UITextField alloc]init];
    self.payTF.frame = CGRectMake(0, 0, 0, 0);
    self.payTF.delegate = self;
    self.payTF.keyboardType=UIKeyboardTypeNumberPad;
    [self.payTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.payView addSubview:self.payTF];
    
    //  假的输入框和假的密码点
    for (int i = 0; i < payNum; i ++) {
        UIView *tempView = [UIView new];
        tempView.frame = CGRectMake((view.frame.size.width-boxWidth*payNum)/2 + boxWidth *i - i * 0.5, CGRectGetMaxY(lable_title.frame)+20, boxWidth, boxWidth);
        tempView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x3C3C3D]];
        tempView.layer.borderColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0xD9D9D9] darkColor:[UIColor colorWithHex:0x4B4C4D]].CGColor;
        [tempView.layer setBorderWidth:0.5];
        if (i == 0) {
            UIRectCorner rectCorner = UIRectCornerTopLeft | UIRectCornerBottomLeft;
            [tempView round:4 RectCorners:rectCorner];
        }
        if (i == payNum-1) {
            UIRectCorner rectCorner = UIRectCornerTopRight | UIRectCornerBottomRight;
            [tempView round:4 RectCorners:rectCorner];
        }
        tempView.tag = 10 + i;
        [self.payView addSubview:tempView];
        
        UILabel *tempLab = [[UILabel alloc]init];
        tempLab.frame = CGRectMake((boxWidth-10)/2, (boxWidth-10)/2, 10, 10);
        [tempLab.layer setCornerRadius:5];
        [tempLab.layer setMasksToBounds:YES];
        tempLab.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x333333] darkColor:[UIColor colorWithHex:0xE0E0E0]];
        [tempLab setHidden:YES];
        [tempView addSubview:tempLab];
    }
    
    // 二级标题
    UILabel *lable_subTitle = [[UILabel alloc]init];
    lable_subTitle.frame = CGRectMake(10, lable_title.frame.origin.y+lable_title.frame.size.height+20+64, view.frame.size.width-20, 20);
    lable_subTitle.text = [NSString stringWithFormat:@"%@",subTitle];
    lable_subTitle.textAlignment=1;
    lable_subTitle.font = [UIFont systemFontOfSize:14.0f];
    lable_subTitle.textColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x999999] darkColor:[UIColor colorWithHex:0x868686]];
    [self.payView addSubview:lable_subTitle];
    if([[NSString stringWithFormat:@"%@",subTitle] isEqualToString:@""]) {
        lable_subTitle.frame = CGRectMake(10, lable_title.frame.origin.y+lable_title.frame.size.height, view.frame.size.width-20, 0);
    }
    [self show];
}

#pragma mark - 文本框内容发生改变
- (void) textFieldDidChange:(UITextField*) sender {
    UITextField *_field = sender;
    
    for (int i = 0; i < self.payNum; i ++) {
        UIView *tempView = (UIView *)[self.payView viewWithTag:10 + i];
        UILabel *tempLab = (UILabel *)[[tempView subviews] firstObject];
        if (i < _field.text.length) {
               tempLab.hidden = NO;
        }else{
            tempLab.hidden = YES;
        }
    }

    if (_field.text.length== self.payNum){
        // 延迟执行
        if (@available(iOS 10.0, *)) {
            [NSTimer scheduledTimerWithTimeInterval:0.3 repeats:NO block:^(NSTimer * _Nonnull timer) {
                [self autoVerifyPassWord:_field.text];
            }];
        } else {
            
        }
    }
}

// 自动验证
- (void)autoVerifyPassWord:(NSString *)passWord{
    [self removeFromView];
    self.verifyPassWordAuto(passWord);
}

- (void)show {
    
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [self.payTF becomeFirstResponder];
    self.payView.transform = CGAffineTransformMakeScale(1.21f, 1.21f);
    [UIView animateWithDuration:.8f delay:0.f usingSpringWithDamping:.7f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.payView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        self.viewBG.alpha = 0.5;
        self.payView.alpha = 1;

    } completion:nil];
}

#pragma mark - 移除
- (void)removeFromView {
    [self.payTF resignFirstResponder];
    self.payView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
    [UIView animateWithDuration:.8f delay:0.f usingSpringWithDamping:.7f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.payView.transform = CGAffineTransformMakeScale(1.21f, 1.21f);
        self.payView.alpha = 0;
        self.viewBG.alpha = 0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
